﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public partial class Test_Default : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
            TxtStamptext.Visible = false;
            FUStamp.Visible = false;
            btnStamp.Visible = false;
        }
    }
    protected void GetData()
    {
        string sqlgetdata = "Select * from TestFileUpload";
        DataTable Dt = DU.getdata(sqlgetdata);
        if (Dt.Rows.Count > 0)
        {
            grdview.DataSource = Dt;
            grdview.DataBind();
        }
    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        string filePath = "";
        if (PdfFile.HasFile)
        {
            filePath = "~/Test/UploadedFile/" + PdfFile.PostedFile.FileName;
            PdfFile.SaveAs(Server.MapPath(filePath));
            SqlParameter[] param = { new SqlParameter("@FileName", filePath) };
            int insert = DU.executeprocedure("Usp_UploadFile", param);
            if (insert > 0)
            {
                GetData();
                Label1.Text = "1 file SuccessFully Uploaded";
                Label1.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
    //PdfReader pdfReader=new PdfReader(Server.MapPath("Input") + "/" + "input.pdf");
    //FileStream stream = new FileStream(Server.MapPath("Output") + "/output.pdf", FileMode.OpenOrCreate);
    //PdfStamper pdfStamper = new PdfStamper(pdfReader,stream);
    //for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
    //{
    //    Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
    //    PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
    //    pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40);
    //    PdfGState graphicsState = new PdfGState();
    //    graphicsState.FillOpacity = 0.4F;
    //    pdfData.SetGState(graphicsState);
    //    pdfData.SetColorFill(BaseColor.BLUE);
    //    pdfData.BeginText();
    //    pdfData.ShowTextAligned(Element.ALIGN_CENTER, "BlueLemonCode", pageRectangle.Width / 2, pageRectangle.Height / 2, 45);
    //    pdfData.EndText();
    //}
    //pdfStamper.Close();
    //stream.Close();
    //protected void lnkButtonVerify_Click(object sender, EventArgs e)
    //{
       
    //}
    protected void rblFormat_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rblFormat.Items[0].Selected == true)
        {
            TxtStamptext.Visible = true;
            FUStamp.Visible = false;
            btnStamp.Visible = false;
        }
        else
        {
            TxtStamptext.Visible = false;
            FUStamp.Visible = true;
            btnStamp.Visible = true;
        }
    }
    //protected void lnkView_Click(object sender, EventArgs e)
    //{

    //    Response.Redirect("~/Test/UploadedFile/CompanyReports11.pdf");
    //}
    protected void grdview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "grdRowView")
        {
            string KeyId = e.CommandArgument.ToString();
            string Sql = "Select * from TestFileUpload where Id='" + KeyId + "'";
            DataTable Dt = DU.getdata(Sql);
            if (Dt.Rows.Count > 0)
            {
                Response.Redirect(Dt.Rows[0]["Filename"].ToString());
                //Response.Redirect("~/Test/UploadedFile/Payslip_00491.pdf");
            }
        }
        if (e.CommandName == "grdVerify")
        {
            string KeyId = e.CommandArgument.ToString();
            string Sql = "Select * from TestFileUpload where Id='" + KeyId + "'";
            DataTable Dt = DU.getdata(Sql);
            if (Dt.Rows.Count > 0)
            {
                string StampDate = System.DateTime.Now.ToString(string.Format("dd/MMM/yyyy"));
                PdfReader PdfReader; FileStream stream; PdfStamper pdfstamper;
                //Label lblFileName = (Label)grdview.Rows[0].FindControl("lblFileName");
                string lblFileName = Dt.Rows[0]["Filename"].ToString();
                try
                {
                    PdfReader = new PdfReader(Server.MapPath(lblFileName));
                    stream = new FileStream(Server.MapPath(lblFileName), FileMode.Open);//new File Is Created.
                    pdfstamper = new PdfStamper(PdfReader, stream);
                    
                    if (TxtStamptext.Text != "")//For text only
                    {
                        for (int pageindex = 1; pageindex <= PdfReader.NumberOfPages; pageindex++)
                        {
                            Rectangle pageRect = PdfReader.GetPageSizeWithRotation(pageindex);
                            PdfContentByte pdfdata = pdfstamper.GetUnderContent(pageindex);
                            PdfContentByte pdfdata1 = pdfstamper.GetUnderContent(pageindex);
                            
                            pdfdata.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED), 20);
                            pdfdata.BeginText();
                            pdfdata.ShowTextAligned(Element.ALIGN_UNDEFINED, TxtStamptext.Text,10,750,0);
                            pdfdata.EndText();

                            pdfdata1.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED), 10);
                            pdfdata1.BeginText();
                            pdfdata1.ShowTextAligned(Element.ALIGN_UNDEFINED, StampDate, 10, 740, 0);
                            pdfdata1.EndText();
                        }
                        pdfstamper.Close();
                        stream.Close();
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    
                }
            }
        }
    }
}